<?php

namespace app\services;

use app\exceptions\SystemException;
use app\model\AuthUsers;

class AuthService extends ApiService
{
    protected $authUsersModel;

    public function __construct(array $userInfo = [], array $params = [])
    {
        parent::__construct($userInfo, $params);
        $this->authUsersModel = new AuthUsers();
    }

    /**
     * 登录
     *
     * @return mixed
     * @throws SystemException
     */
    public function login()
    {
        if (!$userInfo = $this->authUsersModel->loginInfo($this->params['username'], md5($this->params['password']))) {
            throw new SystemException('账号或者密码错误');
        }
        $userInfo['token'] = md5($userInfo['id'] . time());
        //更新token
        $this->authUsersModel->updateTokenById($userInfo['id'], $userInfo['token']);

        return $userInfo;
    }
}